package cn.gztang.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Objects;

/**
 * @author GaoFeng2017
 * @date 2018-02-26 12:27:41
 **/

public class DateUtil {

    /**
     * 获取当前日期
     * @author GaoFeng2017
     * @date 2018/2/26 12:33
     * @return java.time.LocalDate
     *
     */
    public static LocalDate getCurrentDate() {
        return LocalDate.now();
    }

    /**
     * 根据年月日来获取指定日期
     * @author GaoFeng2017
     * @date 2018/2/26 12:37
     * @return java.time.LocalDate
     *
     */
    public static LocalDate getSpecifyDate(int year,int month,int day) {
        return LocalDate.of(year,month,day);
    }

    /**
     * 获取指定日期的指定周后的时间
     * @author GaoFeng2017
     * @date 2018/2/26 12:43
     * @return java.time.LocalDate
     *
     */
    public static LocalDate getNextWeek(LocalDate localDate,int plusWeek) {
        return localDate.plus(plusWeek, ChronoUnit.WEEKS);
    }

    /**
     *  获取两个日期之间相差的天数
     * @author GaoFeng2017
     * @date 2018/2/26 12:52
     * @return int
     *
     */
    public static int getDiffDays(int aYear,int aMonth,int aDay
            ,int bYear,int bMonth,int bDay) {

        Calendar beginTime = Calendar.getInstance();
        beginTime.set(aYear,aMonth - 1,aDay,0,0,0);
        Calendar endTime = Calendar.getInstance();
        endTime.set(bYear,bMonth - 1,bDay,0,0,0);

        long aMillis = beginTime.getTimeInMillis();
        long bMillis = endTime.getTimeInMillis();

        int days = (int) ((bMillis - aMillis) / (24*3600*1000));
        return days;
    }

    /**
     * 比较两个日期
     *
     * @author GaoFeng2017
     * @date 2018/2/26 13:14
     *
     * @return int 1,0,-1 大于, 等于 ,小于
     *
     */
    public static int compareTo(LocalDate aLocalDate,LocalDate bLocalDate) {
        if (Objects.equals(aLocalDate,bLocalDate)) {
            return 0;
        }

        if (aLocalDate.isBefore(bLocalDate)) {
            return -1;
        }

        return 1;
    }

    /**
     * 获取日期所在星期
     * @author GaoFeng2017
     * @date 2018/2/26 15:53
     * @param  localDate
     * @return java.lang.String 星期的字符串表现形式
     *
     */
    public static String getWeekday(LocalDate localDate) {
        String[] weekStr = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
        int index = localDate.getDayOfWeek().getValue() - 1;
        return weekStr[index];
    }


    /**
     *
     * 获取两个日期间相差的周数
     *
     * @author ZengZhengFang
     *
     * @date 2018/7/24 15:38
     *
     * @param reportTime
     * @param specifyDate
     *
     * @return int
     *
     */
    public static int getWeekBySpecifyDate(LocalDate reportTime,LocalDate specifyDate) {

        int days = DateUtil.getDiffDays(
                reportTime.getYear(),
                reportTime.getMonthValue(),
                reportTime.getDayOfMonth(),
                specifyDate.getYear(),
                specifyDate.getMonthValue(),
                specifyDate.getDayOfMonth());

        int weekday = reportTime.getDayOfWeek().getValue();
        days = days - (8 - weekday);

        if (days < 0) {
            return 1;
        }

        return days/7 + 2;
    }

    /**
     * 格式化日期
     * @author ZengZhengFang
     *
     * @date 2018/7/24 15:39
     *
     * @param localDate
     * @param fromatStr
     *
     * @return java.lang.String
     *
     */
    public static String dateFormat(LocalDate localDate,String fromatStr) {

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(fromatStr);
        return dateTimeFormatter.format(localDate);
    }

    /**
     *
     * 获取当前时间的秒值
     *
     * @author ZengZhengFang
     *
     * @date 2018/7/24 15:46
     *
     * @param
     *
     * @return java.lang.Long
     *
     */
    public static Long getSecond() {

        return Instant.now().getEpochSecond();
    }

    /**
     *
     * 获取当前时间的毫秒值
     *
     * @author ZengZhengFang
     *
     * @date 2018/7/24 15:46
     *
     * @param
     *
     * @return java.lang.Long
     *
     */
    public static Long now() {
        return System.currentTimeMillis();
    }
}
